class tongueMgr extends eui.Component {
	public constructor(game, frogMgr: frogMgr) {
		super();
		this.gameHandle = game;
		this.m_frogMgr = frogMgr;
		this.init();
	}
	/**舞台 */
	private gameHandle;
	/**舌头对象池 */
	public tonguePool: tongue[] = [];
	/**对象池中舌头的总数量 */
	private MaxCount: number = 20;
	/***青蛙的管理类 */
	private m_frogMgr: frogMgr;


	/**当前服务器房间所有的线 */
	public nowServerAllLine;
	/**服务器发送过来的玩家的信息 */
	private userData;

	private tongueTimer = new egret.Timer(1000 / 60, 0);
	/**红心表情是否存在 */
	private happeFaceBool = false;
	/**定时器 */
	private timer: egret.Timer
	/**对象池初始化 */
	public init() {
		this.addTongueIntoPool();
		this.tongueTimer.addEventListener(egret.TimerEvent.TIMER, this.updataTongue, this);
		this.tongueTimer.start();
	}
	/**向对象池中添加指定数量的舌头对象 */
	private addTongueIntoPool() {
		for (var i = 0; i < this.MaxCount; i++) {
			this.createOneTongue();
		}
	}
	/**生成一个舌头 */
	private createOneTongue() {
		var tongueObj = new tongue();
		this.tonguePool.push(tongueObj);
		this.gameHandle.addChild(tongueObj);
	}
	//从对象池中获取一个舌头
	private getOneTongue() {
		var tongue = null;
		for (var i = 0; i < this.tonguePool.length; i++) {
			if (!this.tonguePool[i].isUsed) {
				tongue = this.tonguePool[i];
				break;
			}
		}
		if (tongue == null) {
			console.log("线池中没线....");
			//	this.createOneTongue();
			tongue = this.tonguePool.length - 1;
		}
		return tongue;
	}


	/**舌头数据更新 */
	public gameTongueDataUpdata(userData: any, lineData: any) {
		this.nowServerAllLine = lineData;
		this.userData = userData;
		if (lineData.length == 0) {
			this.resetAllTongue();
			return;
		}
		this.lineUpdata();
	}

	/**更新线/检查删除和增加*/
	private lineUpdata() {
		/**检查需要删除的 */
		for (var t = 0; t < this.tonguePool.length; t++) {
			if (this.isCheckLineNeedRemove(this.tonguePool[t])) {
				//	this.addOneLineye(this.tonguePool[t]);
				this.tonguePool[t].reset();
			}
		}

		/**检查需要添加的 */
		for (var t = 0; t < this.nowServerAllLine.length; t++) {
			if (this.isCheckLineNeedAdd(this.nowServerAllLine[t])) {
				this.addOneLin(this.nowServerAllLine[t]);
			}
		}
	}

	/**检查线是否需要删除*/
	private isCheckLineNeedRemove(line: any) {
		for (var t = 0; t < this.nowServerAllLine.length; t++) {
			/**判断是否由当前线 */
			if ((this.nowServerAllLine[t].userOneId == line.userOneId && this.nowServerAllLine[t].userTwoId == line.userTwoId))
				return false;
			else if (this.nowServerAllLine[t].userOneId == line.userTwoId && (this.nowServerAllLine[t].userTwoId == line.userOneId))
				return false;
		}
		return true;
	}

	/**检查是否需要添加线 */
	private isCheckLineNeedAdd(line: any) {
		for (var t = 0; t < this.tonguePool.length; t++) {
			/**判断是否由当前线 */
			if ((this.tonguePool[t].userOneId == line.userOneId && this.tonguePool[t].userTwoId == line.userTwoId))
				return false;
			else if (this.tonguePool[t].userOneId == line.userTwoId && (this.tonguePool[t].userTwoId == line.userOneId))
				return false;
		}
		return true;
	}
	private happeFaceBoolChange() {
		this.happeFaceBool = false;
	}
	/**添加一条线 */
	private addOneLin(oneLineData) {

		var tongue_1 = this.getOneTongue();
		var startPos = this.m_frogMgr.findFrogById(oneLineData.userOneId);
		var endPos = this.m_frogMgr.findFrogById(oneLineData.userTwoId);
		if (endPos != null && startPos != null) {//玩家连线的时候玩家添加表情
			startPos.changeFaceEye(2, null);
			endPos.changeFaceEye(2, null);
			this.happeFaceBool = true;
			egret.setTimeout(function (arg) {
				this.happeFaceBool = false;
			}, this, 2000);
		}
		if (startPos == null) {
			startPos = oneLineData.uop;
		}
		if (endPos == null) {
			endPos = oneLineData.utp;
		}
		var newLineData = this.getOneLineData(startPos, endPos);
		tongue_1.born(newLineData, oneLineData.userOneId, oneLineData.userTwoId);
	}

	/**更新一条线 */
	private updataTongue() {

		let startPos;
		let endPos;
		let jv;
		let nowLineData;
		let TongueMax;
		let newLineData;
		let tongueTmp;
		for (let i = 0; i < this.tonguePool.length; i++) {
			tongueTmp = this.tonguePool[i];
			if (tongueTmp.isUsed == false) {
				continue;
			}
			startPos = this.m_frogMgr.findFrogById(tongueTmp.userOneId);
			endPos = this.m_frogMgr.findFrogById(tongueTmp.userTwoId);
			if (startPos == null || endPos == null) {
				if (startPos != null) {
					startPos.frogIsHadLine = false;

				}
				if (endPos != null) {
					endPos.frogIsHadLine = false;

				}

				tongueTmp.reset();
				return;
			}
			else {
				jv = startPos.frogLineLength + endPos.frogLineLength;
				if (Util.getInstance().getDistance(startPos, endPos) > jv * 0.8) {//监听并判断线长//00修改，原jv - 150改为0.7*jv
					//	nowLineData = this.getOneLineData(startPos, endPos);
					/**快断线眼 */
					startPos.changeFaceEye(0, jv);
					endPos.changeFaceEye(0, jv)
					//FaceManager.GetInstance().ChangeWinkEyes(startPos,startPos.leftEye,startPos.RightEye);
					//FaceManager.GetInstance().ChangeWinkEyes(endPos,startPos.leftEye,startPos.RightEye);
				}	
				/**正常眼*/			
				else{
					startPos.changeFaceEye(1,null);
					endPos.changeFaceEye(1,null);
				}
				//
				// 	if (!this.happeFaceBool) {
				// 	//	startPos.changeFaceEye(1, null);
				// 	//	endPos.changeFaceEye(1, null);
				// 	}
				// }
			}
			TongueMax = startPos.frogLineLength + endPos.frogLineLength;//00修改获取舌头的最大长度
			newLineData = this.getOneLineData(startPos, endPos);
			tongueTmp.updateTongueData(newLineData, TongueMax);
		}
		// this.tonguePool.forEach(e => {
		// 	if (e.isUsed) {
		// 		startPos = this.m_frogMgr.findFrogById(e.userOneId);
		// 		endPos = this.m_frogMgr.findFrogById(e.userTwoId);
		// 		if (endPos != null && startPos != null) {
		// 			jv = startPos.frogLineLength + endPos.frogLineLength;
		// 			if (Util.getInstance().getDistance(startPos, endPos) > jv * 0.8) {//监听并判断线长//00修改，原jv - 150改为0.7*jv
		// 				nowLineData = this.getOneLineData(startPos, endPos);
		// 				startPos.changeFaceEye(0, jv);
		// 				endPos.changeFaceEye(0, jv)
		// 			}
		// 			else {
		// 				if (!this.happeFaceBool) {
		// 					startPos.changeFaceEye(1, null);
		// 					endPos.changeFaceEye(1, null);
		// 				}
		// 			}
		// 		}
		// 		if (startPos == null)
		// 			startPos = this.findTonguePosInServer(e);
		// 		if (endPos == null)
		// 			endPos = this.findTonguePosInServer(e);
		// 		if (endPos == null || startPos == null)
		// 			return;
		// 		TongueMax = startPos.frogLineLength + endPos.frogLineLength;//00修改获取舌头的最大长度
		// 		newLineData = this.getOneLineData(startPos, endPos);
		// 		e.updateTongueData(newLineData, TongueMax);
		// 	}
		// });
	}

	/**根据起始坐标获取线所需要的信息 */
	private getOneLineData(startPos, endPos) {

		return {
			type: startPos.nowPropsType,
			postion: Util.getInstance().getCenterPos(startPos, endPos),
			rotation: Util.getInstance().getRotation(startPos, endPos),
			lineLenth: Util.getInstance().getDistance(startPos, endPos),
		}
	}
	//根据id查找服务器上舌头的位置
	private findTonguePosInServer(e) {
		var data = null;
		for (var i = 0; i < this.nowServerAllLine.length; i++) {
			if (this.nowServerAllLine[i].userOneId == e.userOneId && this.nowServerAllLine[i].userTwoId == e.userTwoId) {
				data = { uop: this.nowServerAllLine[i].uop, utp: this.nowServerAllLine[i].utp };
			} else if (this.nowServerAllLine[i].userOneId == e.userTwoId && this.nowServerAllLine[i].userTwoId == e.userOneId) {
				data = { uop: this.nowServerAllLine[i].utp, utp: this.nowServerAllLine[i].uop };
			}
		}
		if (data == null) {
			console.log("服务器上一次发送过来的lineData中竟然没有这个舌头");
			//没有这个舌头**舌头初始化
			e.reset();

		}
		return data;
	}

	//重置所有的舌头
	private resetAllTongue() {
		this.tonguePool.forEach(e => {
			if (e.isUsed) {
				e.reset();
			}
		});
	}


	// //计算这条舌头的最大长度
	// private calculateMaxLength(userData, line) {
	// 	var length_1 = this.findLineLength(userData, line.userOneId);
	// 	var length_2 = this.findLineLength(userData, line.userTwoId);
	// 	var totalLength = -1;
	// 	if (length_1 == null || length_2 == null) {
	// 		console.log("和玩家相连的青蛙在屏幕外，无法得知这只青蛙的舌头的长度");
	// 		length_1 = length_1 == null ? 0 : length_1;
	// 		length_2 = length_2 == null ? 0 : length_2;
	// 	}
	// 	totalLength = length_1 + length_2;
	// 	return totalLength;
	// }
	// //根据id查找 这只青蛙舌头的最大长度
	// private findLineLength(userData, id) {
	// 	var length = null;
	// 	for (var i = 0; i < userData.length; i++) {
	// 		if (userData[i].userId == id) {
	// 			length = userData[i].lineLength;
	// 			break;
	// 		}
	// 	}
	// 	return length;
	// }
}